package com.um.jdy.common.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.um.jdy.common.activity.code.CouponsCode;
import com.um.jdy.common.activity.dao.CouponsDao;
import com.um.jdy.common.activity.entity.po.Coupons;
import com.um.jdy.common.activity.entity.vo.CouponsUserDetailVO;
import com.um.jdy.common.activity.entity.vo.CouponsVO;
import com.um.jdy.common.activity.entity.vo.StationAvailableCouponsVO;
import com.um.jdy.common.activity.enums.ActivityEnum;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.springboot.starter.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.um.springboot.starter.orm.service.BaseEntityServiceImpl;
import com.um.jdy.common.activity.service.CouponsUserService;
import com.um.jdy.common.activity.dao.CouponsUserDao;
import com.um.jdy.common.activity.entity.po.CouponsUser;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类：优惠券用户关联表
 */
@Service
@Transactional
@Slf4j
public class CouponsUserServiceImpl extends BaseEntityServiceImpl<CouponsUserDao,CouponsUser>  implements CouponsUserService{

    @Autowired
    CouponsDao couponsDao;

    /**
     * 查询可用优惠券列表
     *
     * @param deviceStationId
     * @param deviceType 设备类型
     * @param memberUserId
     * @return
     */
    @Override
    public List<StationAvailableCouponsVO> getStationAvailableList(String deviceStationId,String deviceType, String memberUserId) {
        List<StationAvailableCouponsVO> list = getViewModelList("get_available_coupons_list", StationAvailableCouponsVO.class,
                new Object[]{deviceStationId, memberUserId});
        List<StationAvailableCouponsVO> newList = list.stream().filter(item ->
                ActivityEnum.UseRange.All.name().equals(item.getAvailableDevice()) ||
                        JSONUtil.parseArray(item.getDeviceTypes()).contains(deviceType)).collect(Collectors.toList());
        return newList;
    }

    /**
     * 查询充电可用优惠券列表
     *
     * @param deviceStationId
     * @param deviceType 设备类型
     * @param memberUserId
     * @param amount
     * @return
     */
    @Override
    public List<CouponsVO> getChargingAvailableList(String deviceStationId,String deviceType, String memberUserId, Number amount) {
        List<CouponsVO> list = getViewModelList("get_charging_available_coupons_list", CouponsVO.class,
                new Object[]{deviceStationId,getComId(), memberUserId, amount});

        List<CouponsVO> newList = list.stream().filter(item ->
                ActivityEnum.UseRange.All.name().equals(item.getAvailableDevice()) ||
                        JSONUtil.parseArray(item.getDeviceTypes()).contains(deviceType)).collect(Collectors.toList());
        return newList;
    }

    /**
     * 查询充电可用且未领取的优惠券列表
     *
     * @param deviceStationId
     * @param deviceType 设备类型
     * @param memberUserId
     * @param amount
     * @return
     */
    @Override
    public List<CouponsVO> getChargingAvailableNotRedeemedList(String deviceStationId,String deviceType, String memberUserId, Number amount) {
        List<CouponsVO> list = getViewModelList("get_charging_available_not_redeemed_coupons_list", CouponsVO.class,
                new Object[]{deviceStationId,getComId(), amount, deviceStationId, memberUserId, amount, memberUserId});

        List<CouponsVO> newList = list.stream().filter(item ->
                ActivityEnum.UseRange.All.name().equals(item.getAvailableDevice()) ||
                        JSONUtil.parseArray(item.getDeviceTypes()).contains(deviceType)).collect(Collectors.toList());
        return newList;
    }

    /**
     * 查询用户优惠券
     *
     * @param activityCouponsUserId
     * @return
     */
    @Override
    public CouponsUserDetailVO getUserCoupons(String activityCouponsUserId) {
        CouponsUser couponsUser = dao.getRow(new String[]{"activity_coupons_user_id"},
                new String[]{activityCouponsUserId});
        if(null == couponsUser){
            return null;
        }

        Coupons coupons = couponsDao.getRow(couponsUser.getActivityCouponsId());
        CouponsUserDetailVO couponsUserDetailVO = BeanUtil.toBean(coupons, CouponsUserDetailVO.class);
        couponsUserDetailVO.setActivityCouponsUserId(couponsUser.getActivityCouponsUserId());
        return couponsUserDetailVO;
    }

    /**
     * 查询用户优惠券
     *
     * @param activityCouponsId
     * @param userId
     * @return
     */
    @Override
    public CouponsUserDetailVO getUserCoupons(String activityCouponsId, String userId) {
        CouponsUser couponsUser = dao.getRow(new String[]{"activity_coupons_id", "member_user_id", "is_use"},
                new String[]{activityCouponsId, userId, YesOrNo.No.name()});
        if(null == couponsUser){
            return null;
        }

        Coupons coupons = couponsDao.getRow(couponsUser.getActivityCouponsId());
        CouponsUserDetailVO couponsUserDetailVO = BeanUtil.toBean(coupons, CouponsUserDetailVO.class);
        couponsUserDetailVO.setActivityCouponsUserId(couponsUser.getActivityCouponsUserId());
        return couponsUserDetailVO;
    }

    /**
     * 退回优惠券已使用
     *
     * @param activityCouponsUserId
     * @return
     */
    @Override
    public boolean returnCouponsUse(String activityCouponsUserId) {
        if(StrUtil.isEmpty(activityCouponsUserId)){
            return true;
        }

        boolean result = true;

        //修改已领取优惠券为未使用状态
        CouponsUser couponsUser = getRow(activityCouponsUserId);
        if(null != couponsUser){
            couponsUser.setIsUse(YesOrNo.No.name());
            result = this.saveRow(couponsUser);
        }

        return result;
    }

    /**
     * 保存优惠券已使用
     *
     * @param activityCouponsUserId
     * @return
     */
    @Override
    public boolean saveCouponsUse(String activityCouponsUserId) {
        if(StrUtil.isEmpty(activityCouponsUserId)){
            return false; //防止调用处报NULL
        }
        CouponsUser couponsUser = getRow(activityCouponsUserId);
        if(null == couponsUser){
            throw new ServiceException(CouponsCode.CouponsNotExist);//优惠券已失效
        }

        Coupons coupons = couponsDao.getRow(couponsUser.getActivityCouponsId());
        if(null == coupons){
            throw new ServiceException(CouponsCode.CouponsNotExist);//优惠券已失效
        }

        //保存优惠券已使用
        couponsUser.setIsUse(YesOrNo.Yes.name());
        return this.saveRow(couponsUser);
    }

}
